﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.Framework.WinTest.Models;
using ZhCun.Framework.Common.Models.WhereHelperModel;

namespace ZhCun.Framework.WinTest
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        /// <summary>
        /// bll对象
        /// </summary>
        BLLCommon _BllObj;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            _BllObj = new BLLCommon();
        }
       
        private void btnGetList_Click(object sender, EventArgs e)
        {
            WhereHelper wh1 = _BllObj.CreateWhereHelper();
            List<Test1> list1 = _BllObj.GetList<Test1>(wh1);
            dataGridView1.DataSource = list1;

            WhereHelper wh2 = _BllObj.CreateWhereHelper();
            List<Test2> list2 = _BllObj.GetList<Test2>(wh2);
            dataGridView2.DataSource = list2;
        }

        private void btnGetDataTable_Click(object sender, EventArgs e)
        {
            WhereHelper wh1 = _BllObj.CreateWhereHelper();
            DataTable list1 = _BllObj.GetDataTable<Test1>(wh1);
            dataGridView1.DataSource = list1;

            WhereHelper wh2 = _BllObj.CreateWhereHelper();
            DataTable list2 = _BllObj.GetDataTable<Test2>(wh2);
            dataGridView2.DataSource = list2;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Test1 t1 = new Test1();
            Test2 t2 = new Test2();
            t1.Name = txtName.Text;
            t1.Age = txtAge.Text;
            t1.Remark = txtRemark.Text;
            t2.Name = txtName.Text;
            t2.Age = txtAge.Text;
            t2.Remark = txtRemark.Text;
            try
            {
                _BllObj.TransStart();
                _BllObj.Add(t2);
                _BllObj.Add(t1);
                var model = _BllObj.GetModel<Test1>(1);
                if (model != null)
                {
                    model.Remark = "更新时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    _BllObj.Update(new string[] { "Remark" }, model);
                }
                
                _BllObj.TransCommit();
                MessageBox.Show("提交成功!");
            }
            catch (Exception ex)
            {
                _BllObj.TransRollback();
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                object idValue = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                var model = _BllObj.GetModel<Test1>(idValue);
                txtAge.Text = model.Age;
                txtName.Text = model.Name;
                txtRemark.Text = model.Remark;
                btnUpdateTest1.Tag = idValue;
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                object idValue = dataGridView2.Rows[e.RowIndex].Cells[0].Value;
                var model = _BllObj.GetModel<Test2>(idValue);
                txtAge.Text = model.Age;
                txtName.Text = model.Name;
                txtRemark.Text = model.Remark;
                btnUpdateTest2.Tag = idValue;
            }
        }

        private void btnUpdateTest1_Click(object sender, EventArgs e)
        {
            if (btnUpdateTest1.Tag == null)
            {
                MessageBox.Show("没有更新的记录");
                return;
            }
            Test1 model = new Test1();
            model.Id = Convert.ToInt32(btnUpdateTest1.Tag);
            model.Name = txtName.Text;
            model.Age = txtAge.Text;
            model.Remark = txtRemark.Text;
            _BllObj.Update(null, model);
            btnUpdateTest1.Tag = null;
            MessageBox.Show("OK");
        }

        private void btnUpdateTest2_Click(object sender, EventArgs e)
        {
            if (btnUpdateTest1.Tag == null)
            {
                MessageBox.Show("没有更新的记录");
                return;
            }
            Test2 model = new Test2();
            model.Id = btnUpdateTest2.Tag.ToString();
            model.Name = txtName.Text;
            model.Age = txtAge.Text;
            model.Remark = txtRemark.Text;
            _BllObj.Update(null, model);
            btnUpdateTest2.Tag = null;
            MessageBox.Show("OK");
        }
    }
}